package com.p2p.cms.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.p2p.cms.dao.CategoryDao;

@Service
public class CategoryService {

	@Autowired
	private CategoryDao categoryDao;

	/******************* 查询 *****************/
	public Map<String, Object> get(Integer id) {
		return categoryDao.get(id);
	}

	// 某个栏目下的分类和分类下的内容数目
	public List<Map<String, Object>> listAll() {
		 List<Map<String, Object>> parentCategoryList= categoryDao.listAllParent();
		 List<Map<String, Object>> childCategoryList= categoryDao.listAllChild();
		 for(Map<String,Object> parentCategory:parentCategoryList){
			 Object idObject = parentCategory.get("id");
			 Integer id= Integer.parseInt(idObject.toString());
			List<Map<String, Object>> currentChildCategoryList = findChild(id,childCategoryList);
			parentCategory.put("childList", currentChildCategoryList);
		 }
		 return parentCategoryList;
	}

	private List<Map<String, Object>> findChild(Integer id,
			List<Map<String, Object>> childCategoryList) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>> ();
		 for(Map<String,Object> child:childCategoryList){
			 if(child.get("parent_id").equals(id)){
				 list.add(child);
			 }
		 }
		return list;
	}

	/******************* 修改 *****************/
	public void add(Map<String, Object> params) {
		Integer sort=MapUtils.getInteger(params, "sort");
		if(sort == null){
			params.put("sort", 100);
		}
		categoryDao.add(params);
	}

	public void update(Map<String, Object> params) {
		categoryDao.update(params);
	}

	public void remove(Integer id) {
		categoryDao.remove(id);
	}

	public Integer getCount(Integer id) {
		return categoryDao.getCount(id);
	}
	
	public Integer getChildCount(Integer id) {
		return categoryDao.getChildIdList(id).size();
	}
	
	//获取一个顶级分类下所有内容的数目
	public Integer getCountIn(Integer id) {
		List<Integer> list=categoryDao.getChildIdList(id);
		list.add(id);
		String idList=list2String(list, ",");
		return categoryDao.getCountIn(idList);
	}
	
	public static String list2String(List<?> list, String separator) {
	
		StringBuilder builder = new StringBuilder();

		int size = list.size();
		for (int index = 0; index < size; index++) {
			if (index == 0) {
				Object str = list.get(index);
				builder.append(str);
			} else {
				Object str = separator + list.get(index);
				builder.append(str);
			}
		}
		String finalStr = builder.toString();
		return finalStr;

	}

	public List<Map<String, Object>> listAllChild() {
		return categoryDao.listAllChild();
	}

	public List<Map<String, Object>> listAllForEdit() {
		return categoryDao.listAllForEdit();
	}

	public List<Integer> getChildIdList(Integer id){
		return this.categoryDao.getChildIdList(id);
	}
	
	public List<Map<String,Object>> getChildSimpleList(Integer id){
		return categoryDao.getChildSimpleList(id);
	}
}
